package cms.simmytech.web;

import cms.simmytech.model.TbContent;
import cms.simmytech.service.ContentService;
import cms.simmytech.web.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping(value = "/contenttest")
public class TbContentController extends BaseController {

    @Autowired
    public ContentService contentService;

    @RequestMapping(value = "/contentList")
    public String contentList(@RequestParam(value = "currentPage", defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize, Model model) {
        PageInfo<TbContent> pageInfo = contentService.getContentList(pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        return super.toPage("content/contentList", model);
    }



    @RequestMapping(value = "/contentDetail")
    public String contentDetail(@RequestParam(value = "contentId") Integer contentId, Model model) {
        TbContent tbContent=contentService.getContentByContentID(contentId);
        model.addAttribute("tbContent",tbContent);
        return super.toPage("content/contentDetail", model);
    }
    @RequestMapping(value = "/contentUpdate")
    public String contentUpdate(TbContent tbContent, Model model) {
        contentService.updateContentByContentId(tbContent);
        return "redirect:contentList";
    }
    @RequestMapping(value = "/contentDelete")
    public String contentDelete(@RequestParam (value = "contentId")Integer contentId,Model model) {
        contentService.deleteContent(contentId);
        return "redirect:contentList";
    }
    @RequestMapping(value = "/to_contentAdd")
    public String toContentAdd(Model model) {

        return super.toPage("content/contentAdd", model);
    }
    @RequestMapping(value = "/contentAdd")
    public String contentAdd(TbContent tbContent,Model model) {
        tbContent.setCreateTime((int)System.currentTimeMillis());
        tbContent.setReadCount(0);
        tbContent.setContentState((byte)0);
        contentService.AddContent(tbContent);
        //return super.toPage("/content/contentList", model);
        return "redirect:contentList";
        //return "content/contentList";
    }
}
